package com.wl.api.controller.system;

import java.util.Date;
import java.util.Objects;
import java.util.function.Predicate;

import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wl.api.common.utils.BeanUtils;
import com.wl.api.common.utils.ErrorCodeEnum;
import com.wl.api.common.utils.JsonObject;
import com.wl.api.common.utils.StringUtil;
import com.wl.api.controller.base.CommonController;
import com.wl.api.dto.base.CommonUserDTO;
import com.wl.api.dto.system.SecurityChannelAreaServiceDTO;
import com.wl.api.dto.system.SecurityChannelAreaServiceNetworkDTO;
import com.wl.api.model.system.SecurityChannelAreaService;
import com.wl.api.model.system.SecurityChannelAreaServiceNetwork;
import com.wl.api.model.system.SecurityChannelAreaServicePartner;
import com.wl.api.service.system.ISecurityChannelAreaLadderPriceService;
import com.wl.api.service.system.ISecurityChannelAreaServiceNetworkService;
import com.wl.api.service.system.ISecurityChannelAreaServicePartnerService;
import com.wl.api.service.system.ISecurityChannelAreaServiceService;
import com.wl.api.service.system.ISecurityChannelService;


/**
 * 
 * @author cj
 *   区域ID --> 区域服务类型(feePickup上门取件费, selfDelivery 自送网点 ) --> 上门取件服务合作者--> 价格阶梯
 *  														 -->  自送网点地址 --> 价格阶梯				
 *  
 * @version 区域服务类型 --> 上门取件服务定义-- 价格阶梯
 */
@RestController
@RequestMapping("/admin/area/serviceSelfDelivery")
public class AdminAreaNetworkController extends CommonController {

	@Autowired
	ISecurityChannelAreaServiceService channelAreaServiceService;
	@Autowired
	ISecurityChannelAreaServiceNetworkService channelAreaServiceNetworkService;
	@Autowired
	ISecurityChannelAreaServicePartnerService channelAreaServicePartnerService;
	@Autowired
	ISecurityChannelAreaLadderPriceService channelAreaLadderPriceService;
	@Autowired
	ISecurityChannelService channelService;
	
	
	/***
	 *  
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/manage", method=RequestMethod.POST)
	public JsonObject create(@RequestBody SecurityChannelAreaServiceNetworkDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "", code = "";
		if (validate().or(s -> s.getId() != null).test(param)) {
			title = ErrorCodeEnum.param_error.getMessage();
			code = ErrorCodeEnum.param_error.getError_code();
			pass = false;
		}
		if(pass) {
			init(param);
			//SecurityChannelAreaServiceNetwork entity = BeanUtils.copyProperties(param, SecurityChannelAreaServiceNetwork.class);
			
			json.setData(channelAreaServiceNetworkService.addServiceNetwork(param));
		}
		setJsonMessage(json, title, code, pass);
		return json;
	}
	/**
	 *  修改数据
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/manage", method = RequestMethod.PUT)
	public JsonObject modify(@RequestBody SecurityChannelAreaServiceNetworkDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "" , error_code = ""; 
		if (validate().or(s -> s.getServiceId() == null).or(s -> s.getId() == null).test(param)) {
			title = ErrorCodeEnum.param_error.getMessage();
			error_code = ErrorCodeEnum.param_error.getError_code();
			pass = false;
		}
		if(pass) {
			//channelAreaServiceService.updateById(BeanUtils.copyProperties(param, SecurityChannelAreaService.class));
			json.setData(channelAreaServiceNetworkService.updateServiceNetwork(param));
		}
		setJsonMessage(json, title, error_code, pass);
		return json;
	}
	/** 只提供修改数据状态 */
	@RequestMapping(value = "/manage/{id}/{status}", method = RequestMethod.PUT)
	public JsonObject modifyStatus(@PathVariable("id")Integer id, @Param("status") Integer status) throws Exception {
		JsonObject json = JsonObject.getInstance();
		CommonUserDTO user = getCommonUser();
		channelAreaServiceNetworkService.updateById(SecurityChannelAreaServiceNetwork.builder()
				.id(id).status(status).updateTime(new Date()).updateUser(user.getUsername()).build());
		setJsonMessage(json);
		return json;
	}
	
	 @RequestMapping(value="/manage/{id}", method = RequestMethod.DELETE)
	public JsonObject del(@PathVariable("id") Integer id) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "" , error_code = ""; 
		CommonUserDTO user = getCommonUser();
		channelAreaServiceNetworkService.deleteById(id);
		setJsonMessage(json, title, error_code, pass);
		return json;
	}
	 @RequestMapping(value="/manage/{id}", method = RequestMethod.GET)
	 public JsonObject getDetail(@PathVariable("id") Integer id) throws Exception {
		 getCommonUser();
		 JsonObject json = JsonObject.getInstance();
		 json.setData(channelAreaServiceNetworkService.getDetail(id));
		 setJsonMessage(json);
		 return json;
	 }
	 
	 @RequestMapping(value="/manage", method = RequestMethod.GET)
	 public JsonObject getItemList(SecurityChannelAreaServiceNetworkDTO param) throws Exception  {
		 JsonObject json = JsonObject.getInstance();
		 getCommonUser();
		 param.buildStartTime(param.getStartTime()).buildEndTime(param.getEndTime()).buildPage(param.getPage(), param.getPageSize());
		 json.setPage(channelAreaServiceNetworkService.getNetworkList(param));
		 setJsonMessage(json);
		 return json;
	 }
	 
	 
	private void init(SecurityChannelAreaServiceNetworkDTO param) throws Exception {
		CommonUserDTO user = getCommonUser();
		if (Objects.isNull(param.getStatus())) {
			param.setStatus(1);
		}
		param.buildCreateMsg(user.getId(), user.getUsername());
	}
	
	private void updateParam(SecurityChannelAreaServiceDTO param) throws Exception {
		CommonUserDTO user = getCommonUser();
		if (Objects.isNull(param.getStatus())) {
			param.setStatus(1);
		}
		param.buildUpdateMsg(user.getUsername());
	}
	 
	 
	private Predicate<SecurityChannelAreaServiceNetworkDTO> validate() {
		/*return  s -> StringUtil.isEmpty(s.getName()) || StringUtil.isEmpty(s.getLogoImg())
				|| StringUtil.isEmpty(s.getChannelDesc()) || ObjectUtil.isEmpty(s.getCostTimeFrom()) ||
				ObjectUtil.isEmpty(s.getReachAreaId()) || ObjectUtil.isEmpty(s.getFromAreaId()) ||
				ObjectUtil.isEmpty(s.getCostTimeTo()) || CollectionUtil.isEmpty(s.getFeeLadderList()) ||
				s.getFeeLadderList().parallelStream().anyMatch(a -> ObjectUtil.isEmpty(a.getAmount())  
						|| ObjectUtil.isEmpty(a.getWeightFrom()) || ObjectUtil.isEmpty(a.getWeightTo()));*/
		return s -> CollectionUtils.isEmpty(s.getLadderList()) 
				|| Objects.isNull(s.getServiceId()) || Objects.isNull(s.getPartnerId())
				|| StringUtil.isEmpty(s.getName()) || StringUtil.isEmpty(s.getLogo()) || StringUtil.isEmpty(s.getDesc())
				|| StringUtil.isEmpty(s.getAddress());
			
				
	}
}
